/**
 * Created by Administrator on 2020/5/11.
 */
var request = require("request");
var mysql = require("mysql");
db = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "xuyanzhao",
    database: "product"
});
//打开数据库
db.connect();
var name = "乐享人生保障（成人女性版）保险";
var table = "accident";
var url = "https://www.allianz360.com/web-ah-backend/productView/determinePremium?";
var years = ["18-20", "21-25", "26-30", "31-35", "36-40", "41-45", "46-50", "51-55"];
//生成产品数据
function genProductInfo() {
    for (i = 0; i < years.length; i++) {
        data = {
            year: years[i],
            days: "1y",
            people_count: "1",
            accident_death: "10000",
            accident_medical: "5000",
            serious_disable: "100000",
            women_special: "100000",
            face_prefix: "10000",
            accident_hospital: "100/天",
            serious_allowance: "200/天",
            airplane_death: "300000",
            train_death: "100000",
            ship_death: "100000",
            bus_death: "50000",
            exigency_back: "100000",
            public_duty: "100000"

        };
        httpRequest(data);
    }
    for (i = 0; i < years.length; i++) {
        data = {
            year: years[i],
            days: "1y",
            people_count: "1",
            accident_death: "20000",
            accident_medical: "10000",
            serious_disable: "200000",
            women_special: "200000",
            face_prefix: "20000",
            accident_hospital: "100/天",
            serious_allowance: "200/天",
            airplane_death: "600000",
            train_death: "200000",
            ship_death: "200000",
            bus_death: "100000",
            exigency_back: "200000",
            public_duty: "200000"

        };
        httpRequest(data);

    }


}
//爬虫请求
function httpRequest(args) {

    if (args["accident_death"] == "10000") {
        planName = "计划一";
        planCode = "CIYOUFAP1";
    } else {
        planName = "计划二";
        planCode = "CIYOUFAP2";
    }

    ages = args["year"].split("-");
    var payload = {
        "ageLimit": {"ageTo": ages[0], "ageFrom": ages[1]},
        "dayLimit": {"dayTo": "366", "dayFrom": "366"},
        "planCode": planCode,
        "planName": planName,
        "productCode": "HET-ADULT-F",
        "productName": "乐享人生-女性版",
        "day": "366"
    };

    request({

        url: url + (new Date().getTime()).toString(),
        method: "POST",
        json: false,
        headers: {
            "Accept": "application/json, text/plain, */*",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
            "Content-Type": "application/json;charset=UTF-8",
            "Cookie": "gr_user_id=1d46c0fb-1e46-4609-8cf7-bfe4fef2f561; grwng_uid=d007488c-1cf8-4b67-b98c-be9e076ca2e8; _ga=GA1.2.406802014.1586228841; rs-id=259589324823482b85fca1a0c3471d88; clientlanguage=zh_CN; XSRF-TOKEN=allianz-b2c-client_b35be859-bfeb-4955-a4b9-2554aff8f9f5; b9b418c361e5a1e4_gr_session_id=8d391c47-0571-46f5-913c-29d94fbd0ae9; b9b418c361e5a1e4_gr_session_id_8d391c47-0571-46f5-913c-29d94fbd0ae9=true; resourceFull=; resource=self; monitor_session=1589181914491; register=ProductDetail%20Page; productCategory_var=%E5%81%A5%E5%BA%B7%E9%99%A9; productClassify_var=%E4%B9%90%E4%BA%AB%E4%BA%BA%E7%94%9F; thirdParty_var=360%E6%B8%A0%E9%81%93; productName_var=%E4%B9%90%E4%BA%AB%E4%BA%BA%E7%94%9F-%E5%A5%B3%E6%80%A7%E7%89%88; Hm_lvt_bc3a4b8dd2f99c27c9b72b2b0b5ec17d=1587965195,1587965405,1588901673,1589181915; Hm_lpvt_bc3a4b8dd2f99c27c9b72b2b0b5ec17d=1589181915; _gid=GA1.2.19829781.1589181916; _gat_UA-30143104-9=1",
            "Sec-Fetch-Dest": "empty",
            "Sec-Fetch-Mode": "cors",
            "Sec-Fetch-Site": "same-origin",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36",
            "X-XSRF-TOKEN": "allianz-b2c-client_b35be859-bfeb-4955-a4b9-2554aff8f9f5"

        },
        body: JSON.stringify(payload)

    }, function (err, res, body) {
        if (!err && res.statusCode == 200) {
            if (body.indexOf("premium") != -1) {
                price = (JSON.parse(body)["premium"].toFixed(2)).toString();
                console.log(args);
                data = {
                    name: name,
                    company: "京东安联",
                    price: price
                };
                data = Object.assign(data, args);
                insertData(data);
            }
        }

    })

}
//插入数据
function insertData(data) {
    STRING = "";
    VALUES = "";
    for (var key in data) {
        STRING += "`" + key + "`" + ",";
        VALUES += '"' + data[key] + '"' + ",";
    }
    fields = ( STRING.substring(0, STRING.length - 1));
    values = ( VALUES.substring(0, VALUES.length - 1));
    Sql = 'INSERT INTO `' + table + '`(' + fields + ')VALUES(' + values + ')'
    db.query(Sql, function (err, result) {
        if (err) {
            console.log("操作失败");
            return;
        }
        console.log(Sql);

    })

}
genProductInfo();